const app = getApp()
var interAd = null
Page({
  data: {
    shareTitle: '',
    shareImage: '',
    timeline_share_id: 0,
    index_type: 'chat',
    content: null,
    token: '',
    active: 1,
    lunboList: [],

    itemList: [],
    noMore: 0,
    page: 1,
  },
  // 获取二维码链接，获取后面的参数
  getUrlParam(url, key) {
    var params = {};
    url = decodeURIComponent(url); //进行base64解码
    if (url.split("?").length <= 1) {
      return '';
    }
    var arr = url.split("?")[1].split("&");
    arr.forEach(function (item) {
      params[item.split("=")[0]] = item.split("=")[1];
    });
    return key ? params[key] : params;
  },
  cuts(e) {
    let num = e.currentTarget.dataset.num;
    if (num == 3) {
      // wx.reLaunch({
      //   url: '/pages/hall/index',
      // })
      app.util.message('努力开发中', 'error', function () {
     
      })
    } else {
      this.setData({
        active: num,
        page: 1,
        itemList: []
      })
      this.getList();
    }
  },
  onLoad(query) {
    let that = this;
    if (query.sid) {
      // 分享id
      wx.setStorageSync('sid', query.sid)
    }
    if (query.q) { //这个函数scene 就是二维码参数
      const src = decodeURIComponent(query.q);
      let sid = that.getUrlParam(src, 'sid')
      // 分享id
      wx.setStorageSync('sid',sid)
    }
    this.getWxappInfo().then(() => {
      app.util.checkLogin().then((res) => {
        if (this.data.index_type == 'chat') {
          this.getList();
          // wx.showLoading({
          //   title: '加载中'
          // })
        }
      })
    })
    this.getconfig();
    this.getLunboList();
  },
  // 配置
  getconfig() {
    app.util.request({
      url: '/huahua/config'
    }).then(res => {
      wx.setStorageSync('typeitem', res.data)
    })
  },
  // 轮播图
  getLunboList() {
    app.util.request({
      url: '/article/getLunboList'
    }).then(res => {
      this.setData({
        lunboList: res.data.list
      })
    })
  },
  // 推荐
  getList() {
    let page = this.data.page;
    let itemList = this.data.itemList;
    let url = '/huahua/indextj';
    if(this.data.active == 2){
      url = '/huahua/index'
    }
    app.util.request({
      url: url,
      data: {
        page: page
      }
    }).then((res) => {
      if (res.data.list.length > 0) {
        this.setData({
          itemList: itemList.concat(res.data.list),
          noMore: 0,
          page: page + 1
        });
      } else {
        this.setData({
          noMore: 1
        });
      }
    });
  },
  refreshList: function () {
    this.setData({
      page: 1,
      itemList: []
    });
    this.getList();
  },
  onPullDownRefresh() {
    this.refreshList();
    setTimeout(() => {
      wx.hideNavigationBarLoading(); //完成停止加载
      wx.stopPullDownRefresh(); //停止下拉刷新
    }, 500);
  },
  onReachBottom() {
    this.getList();
  },
  detail(e) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/draw/detail?id=' + id,
    })
  },

  tabs(e) {
    let num = e.currentTarget.dataset.num;
    if (num == 1) {
      wx.reLaunch({
        url: '/pages/user/index',
      })
    } else {
      wx.navigateTo({
        url: '/pages/draw/index',
      })
    }
  },
  getWxappInfo() {
    const _this = this
    return new Promise(function (resolve, reject) {
      app.util.request({
        url: '/wxapp/getWxappInfo',
        loading: false
      }).then(res => {
        _this.setData({
          shareTitle: res.data.share_title,
          shareImage: res.data.share_image,
          index_type: res.data.index_type,
          content: res.data.content ? JSON.parse(res.data.content) : ''
        })
        resolve()
      })
    })
  },
  async onShareAppMessage() {//右上角分享
    var share_id = await this.getShareId('wechat')
    console.log('sh', share_id)
    return {
      title: this.data.shareTitle,
      imageUrl: this.data.shareImage,
      path: '/pages/index/index?sid=' + share_id
    }
  },
  onShareTimeline() {//朋友圈
    var share_id = this.data.timeline_share_id
    if (!share_id) {
      this.getShareId('timeline')
    }
    console.log('share_id', share_id)
    return {
      title: this.data.shareTitle,
      imageUrl: this.data.shareImage,
      query: 'sid=' + share_id
    }
  },
  async getShareId(way) {
    var share_id = 0
    await app.util.request({
      url: '/wxapp/doShare',
      data: {
        way: way
      }
    }).then(res => {
      share_id = res.data.share_id
      if (way == 'timeline') {
        this.setData({
          timeline_share_id: share_id
        })
      }
    })
    return share_id
  },
  onAddToFavorites() {//收藏
    return {
      title: this.data.shareTitle,
      imageUrl: this.data.shareImage
    }
  }
})